package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.IncludeOtherException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mappper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.jacoco.agent.rt.internal_f3994fa.core.internal.flow.IFrame;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;


    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void saveDishWithFlavor(DishDto dishDto) {
        //保存菜品信息
        this.save(dishDto);
        //保存菜品口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();

        //需要将菜品的id全部录入进去
        flavors.forEach(
                flavor -> {
                    flavor.setDishId(dishDto.getId());
                }
        );
        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public DishDto dishWithFlavor(Long id) {

        //通过id查询到菜品信息
        Dish dish = this.getById(id);

        //组装条件查询口味
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);

        //获取到该菜品的口味
        List<DishFlavor> flavors = dishFlavorService.list(dishFlavorLambdaQueryWrapper);

        DishDto dishDto = new DishDto();

        //将所有条件组装到组合类中返回
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateDish(DishDto dishDto) {
        //先修改菜品信息
        this.updateById(dishDto);

        //直接根据菜品ID删除所有口味
        LambdaQueryWrapper<DishFlavor> dishDeleteWrapper = new LambdaQueryWrapper<>();
        dishDeleteWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(dishDeleteWrapper);

        //再将口味信息添加
        List<DishFlavor> flavors = dishDto.getFlavors();
        //需要将菜品的id全部录入进去
        flavors.forEach(
                flavor -> {
                    flavor.setDishId(dishDto.getId());
                }
        );

        dishFlavorService.saveBatch(flavors);
        return true;
    }


    @Override
    @Transactional
    public boolean deleteDishWithFlavors(List<Long> ids) {

        //首先判断是否处于出售状态
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId,ids);
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);
        int statusCount = this.count(dishLambdaQueryWrapper);
        if (statusCount > 0){
            throw new IncludeOtherException("该菜品还处于出售状态，请先停售，再删除！");
        }

        //判断是否关联未删除套餐
        List<SetmealDish> list = setmealDishService.list();

        if (list.size() > 0 ){
            LambdaQueryWrapper<SetmealDish> setmealDishWrapper = new LambdaQueryWrapper<>();
            setmealDishWrapper.in(SetmealDish::getDishId,ids);
            List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishWrapper);

            if (setmealDishes.size() > 0){
                throw new IncludeOtherException("该菜品关联未删除套餐，请先删除该套餐！");
            }
        }
        //删除菜品信息
        this.removeByIds(ids);

        //删除口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);

        return true;
    }

    @Override
    public void updateStatusByIds(Integer status,List<Long> ids) {

        //当状态处于0时 说明需要改为停售 需要判断是否有关联套餐
        if (status == 0){
            //判断是否关联出售套餐
            LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
            setmealWrapper.eq(Setmeal::getStatus,1);
            List<Setmeal> setmeals = setmealService.list(setmealWrapper);
            List<Long> stmealIds = setmeals.stream().map(setmeal -> {
                return setmeal.getId();
            }).collect(Collectors.toList());

            if (stmealIds.size() > 0){
                LambdaQueryWrapper<SetmealDish> setmealDishWrapper = new LambdaQueryWrapper<>();
                setmealDishWrapper.in(SetmealDish::getSetmealId,stmealIds);
                setmealDishWrapper.in(SetmealDish::getDishId,ids);
                List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishWrapper);

                if (setmealDishes.size() > 0){
                    throw new IncludeOtherException("该菜品关联出售套餐，请先停售该套餐！");
                }
            }
        }

        //修改状态
        List<Dish> dishes = this.listByIds(ids);
        dishes.stream().forEach(
                dish -> {
                    dish.setStatus(status);
                }
        );

        this.updateBatchById(dishes);
    }
}
